﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Net;
using System.IO;
//using System.Windows.Forms;

namespace Extractor.LibCode
{
    public static class cHTML
    {
        public enum PLAYER_POSITION
        {
            GOALKEEPER,
            DEFENDER,
            MIDFILDER,
            ATTACKER,
            COACH

        }
        
        public static void tesHTML(string sxml, out StringBuilder sb)
        {
            //new XmlTextReader().ReadContentAs(
            WebClient client = new WebClient();
            sb = new StringBuilder();
            //   String htmlCode = client.DownloadString("http://www.scoresway.com/?sport=soccer&page=team&id=662");

            HtmlDocument doc = new HtmlDocument();
            //doc.Load(new StringReader(htmlCode));
            string ss = cCommon.strimLine( System.IO.File.ReadAllLines("D:\\Projek Programming\\2010\\2010\\Extractor\\datanya\\note.htm"));
            
            

            
            //doc.Load("D:\\Projek Programming\\2010\\2010\\Extractor\\datanya\\note.htm");
            doc.Load( new StringReader(ss));

            if (doc != null)
            {
                //foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
                //{
                //    HtmlAttribute att = link.Attributes["href"];
                //    HtmlAttribute att2 = link.Attributes["class"];                 
                //    if(att2!=null)
                //    sb.AppendLine(att.Value +" | "+att2.Value);
                //}

                // HtmlDocument doc = new HtmlDocument();
                ////  doc.Load(path); // or .LoadHtml(html);
                //  HtmlNode node = doc.DocumentNode.SelectSingleNode("//meta[@name='description']");
                //  if (node != null)
                //  {
                //      string desc = node.GetAttributeValue("content", "");
                //      // TODO: write desc somewhere
                //  }
                //HtmlNodeCollection hnc= doc.DocumentNode.SelectNodes("//div");

                //foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//div[@class='squad-position-title group-head']"))
                //{
                //    HtmlNodeCollection cn = link.SelectNodes("//div[@class='squad-player']");
                //}

                //HtmlNodeCollection cnGK;
                List<HtmlNode> cnGK= new List<HtmlNode>();
                List<HtmlNode> cnDF = new List<HtmlNode>();
                List<HtmlNode> cnMF = new List<HtmlNode>();
                List<HtmlNode> cnAT = new List<HtmlNode>();
                List<HtmlNode> cnCH = new List<HtmlNode>();
                PLAYER_POSITION pp = PLAYER_POSITION.GOALKEEPER;



                foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//div"))
                {
                    HtmlAttribute attClass = link.Attributes["class"];
                    if (attClass != null)
                    {
                        if (link.Attributes["class"].Value == "squad-position-title group-head")
                        {

                            if (link.InnerHtml.ToUpper() == "GOALKEEPERS")
                            {
                                pp = PLAYER_POSITION.GOALKEEPER;
                            }
                            else if (link.InnerHtml.ToUpper() == "DEFENDERS")
                            {
                                pp = PLAYER_POSITION.DEFENDER;
                            }
                            else if (link.InnerHtml.ToUpper() == "MIDFIELDERS")
                            {
                                pp = PLAYER_POSITION.DEFENDER;
                            }
                            else if (link.InnerHtml.ToUpper() == "ATTACKERS")
                            {
                                pp = PLAYER_POSITION.DEFENDER;
                            }
                            else if (link.InnerHtml.ToUpper() == "COACH")
                            {
                                pp = PLAYER_POSITION.DEFENDER;
                            }
                        }
                   
                    
                    if (link.Attributes["class"].Value == "squad-player")
                    {
                        if (pp == PLAYER_POSITION.GOALKEEPER)
                        {
                            cnGK.Add(link);
                        }
                        else if (pp == PLAYER_POSITION.DEFENDER)
                        {
                            cnDF.Add(link);
                        }
                        else if (pp == PLAYER_POSITION.MIDFILDER)
                        {
                            cnMF.Add(link);
                        }
                        else if (pp == PLAYER_POSITION.ATTACKER)
                        {
                            cnAT.Add(link);
                        }
                        else if (pp == PLAYER_POSITION.COACH)
                        {
                            cnCH.Add(link);
                        }
                    }
                    }
                }

                if (cnGK.Count > 0)
                {
                    importPlayer(PLAYER_POSITION.GOALKEEPER, cnGK);
                }
                if (cnDF.Count > 0)
                {
                    importPlayer(PLAYER_POSITION.DEFENDER, cnDF);
                }
                if (cnMF.Count > 0)
                {
                    importPlayer(PLAYER_POSITION.MIDFILDER, cnMF);
                }
                if (cnAT.Count > 0)
                {
                    importPlayer(PLAYER_POSITION.ATTACKER, cnAT);
                }
                if (cnAT.Count > 0)
                {
                    importPlayer(PLAYER_POSITION.COACH, cnCH);
                }

                


            }

        }

        public static void importPlayer(PLAYER_POSITION pp, List<HtmlNode> squad)
        {
            StringBuilder sb = new StringBuilder();
            string splayerid="";
            string snickname="";
            splayerid=snickname="";

            foreach (HtmlNode hn in squad)
            {
              //  HtmlNode node = doc.DocumentNode.SelectSingleNode("//meta[@name='description']");
                //hn.SelectNodes("//a[@href='
                //foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
                //Htmlnode node
                string spath=hn.XPath;
                foreach (HtmlNode hnn in hn.ChildNodes)
                {
                    //<span class="name large-link" title="">
                    HtmlAttribute att = hnn.Attributes["class"];
//                    HtmlAttribute att = hnn.Attributes["name large-link"];
                   
                    if (att != null)
                    {
                        if (att.Value == "name large-link")
                        {
                            HtmlNode hp = hnn.ChildNodes[1];
                            att = hp.Attributes["href"];
                            if (att != null)
                            {
                                if (att.Value.Contains("?sport=soccer&page=player&id="))
                                {
                                    splayerid = att.Value.Replace("?sport=soccer&page=player&id=", "");
                                    snickname = hnn.InnerText.Trim();

                                    cPlayer oPlayer = new cPlayer(splayerid,"");
                                    oPlayer.Nickname = snickname;
                                    oPlayer.savePlayer();


                                }
                            }
                        }
                    }

                }
            }

            string s = sb.ToString();
        }
            
    }
}
      